package net.csdn.business.common.domain.common;

import lombok.Data;
import net.csdn.business.common.constant.CommonConstants;
import net.csdn.business.common.enums.ActionEventEnum;
import net.csdn.business.common.utils.BusinessUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;

import java.io.Serializable;

/**
 * @author likun
 * @date 2022/11/17 14:01
 */
@Data
public abstract class ActionEventDto implements Serializable {
    private ActionEventEnum event;

    private String traceId;

    public ActionEventDto(ActionEventEnum event) {
        this.event = event;
        this.traceId = genTraceId();
    }

    public ActionEventDto(ActionEventEnum event, String traceId) {
        this.event = event;
        this.traceId = StringUtils.isBlank(traceId) ? genTraceId() : traceId;
    }

    public ActionEventDto() {

    }

    private String genTraceId() {
        String traceId = MDC.get(CommonConstants.TRACE_ID);
        return StringUtils.isNotBlank(traceId) ? traceId : BusinessUtil.genTraceId();
    }
}